package com.maxd.config;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.maxd.bean.area.AreaInfoTree;
import com.maxd.service.area.IAreaService;
import com.maxd.utils.ApplicationContextUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class GuavaCacheConfig {

    @Bean("areaCache")
    public LoadingCache<String, List<AreaInfoTree>> areaCache() {
        LoadingCache<String, List<AreaInfoTree>> cache = CacheBuilder.newBuilder().maximumSize(1000).refreshAfterWrite(24, TimeUnit.HOURS)
                .build(new CacheLoader<String, List<AreaInfoTree>>() {
                    @Override
                    public List<AreaInfoTree> load(String string) throws Exception {
                        IAreaService areaService = ApplicationContextUtils.getBean(IAreaService.class);
                        return areaService.queryAll();
                    }
                });
        return cache;
    }

}
